﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_ccWasteAudit1_Default : BaseTemplatePage
{
    protected int AuditNo = 1;
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AcmsContext.EnsureLoggedIn(this);

            if (Request.QueryString["Audit"] != null)
            {
                try
                {
                    AuditNo = int.Parse(Request.QueryString["Audit"]);
                }
                catch (FormatException) { }
            }
            if (AuditNo <= 0 || AuditNo > 2)
            {
                AuditNo = 1;
            }

            if (AcmsContext.CurrentPost != null)
            {
                lnkPupilNumbers.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../");
                lnkEnergyAudit.NavigateUrl = ResolveUrl(AcmsContext.CurrentPost.ResolvePath("../energyaudit"));
                lnkAudit1.NavigateUrl = ResolveUrl(AcmsContext.CurrentPost.ResolvePath("?Audit=1"));
                lnkAudit2.NavigateUrl = ResolveUrl(AcmsContext.CurrentPost.ResolvePath("?Audit=2"));
                lnkAnalysis.NavigateUrl = ResolveUrl(AcmsContext.CurrentPost.ResolvePath("../wasteanalysis"));
            }
            DataBind();
        }
    }

    public override void DataBind()
    {
        
        pnlMenuEnabled.Visible = true;
        pnlMenuDisabled.Visible = false;
        pnlWasteEntry.Visible = true;
        pnlWaste1Results.Visible = false;
        bool hasPupilNumbersEntered = false;
       
        ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
        var audits = ClimateChangeChampionsData.CarbonCalculation.GetAllByUserId(context, User.Identity.User.Id);
        if (audits.Count > 0)
        {
            //show 2nd if first completed
            liAudit2Active.Visible = audits.First().WasteAuditCompleted;
            liAudit2Off.Visible = !liAudit2Active.Visible;

            //show anaylsis if 2nd complete
            if (audits.Count > 1)
            {
                liAnalysisActive.Visible = audits.First().WasteAuditCompleted && audits.Skip(1).First().WasteAuditCompleted;
                liAnalysisOff.Visible = !liAnalysisActive.Visible;
            }

            ClimateChangeChampionsData.CarbonCalculation calc = audits.Where(x => x.Audit == AuditNo).SingleOrDefault();
            //check no of pupils has been entered
            if (audits.Count >= 1 && audits.First().NoPupils > 0)
            {
                hasPupilNumbersEntered = true;
            }

            if (calc != null && calc.WasteAuditCompleted != false)
            {
                txt_L_Paper.Text = calc.LandPaper.ToString();
                txt_L_Card.Text = calc.LandCard.ToString();
                txt_L_KFW.Text = calc.LandFood.ToString();
                txt_L_PBC.Text = calc.LandBottles.ToString(); 
                txt_L_PP.Text = calc.LandPackaging.ToString();
                txt_L_Glass.Text = calc.LandGlass.ToString();
                txt_L_Cans.Text = calc.LandCans.ToString();
                txt_L_Other.Text = calc.LandOther.ToString();
             
                txt_R_Paper.Text = calc.RecylePaper.ToString();
                txt_R_Card.Text = calc.RecyleCard.ToString();
                txt_R_KFW.Text = calc.RecyleFood.ToString();
                txt_R_PBC.Text = calc.RecyleBottles.ToString();
                txt_R_PP.Text = calc.RecylePackaging.ToString();
                txt_R_Glass.Text = calc.RecyleGlass.ToString();
                txt_R_Cans.Text = calc.RecyleCans.ToString();
                txt_R_Other.Text = calc.RecyleOther.ToString();
                
                
                lbl_L_KGCO2D.Text = calc.TotalLandCO2PerDay.ToString("0.##");
                lbl_L_IcKGCO2D.Text = calc.TotalLandCO2BalloonsPerDay.ToString("#,###");

                lbl_L_TonCO2Yr.Text = calc.TotalLandCO2PerSchoolYear.ToString("0.##");
                lbl_L_IcTonCO2Yr.Text = calc.TotalLandCO2PlanesPerSchoolYear.ToString("0.##");


                lbl_R_KGCO2D.Text = calc.TotalRecyleCO2PerDay.ToString("0.##");
                lbl_R_IcKGCO2D.Text = calc.TotalRecyleCO2BalloonsPerDay.ToString("#,###");

                lbl_R_TonCO2Yr.Text = calc.TotalRecyleCO2PerSchoolYear.ToString("0.##");
                lbl_R_IcTonCO2Yr.Text = calc.TotalRecyleCO2PlanesPerSchoolYear.ToString("0.##");


                lbl_T_KGCO2D.Text = calc.TotalWasteCO2PerDay.ToString("0.##");
                lbl_T_IcKGCO2D.Text = calc.TotalWasteCO2BalloonsPerWeek.ToString("#,###");

                lbl_T_TonCO2Yr.Text = calc.TotalWasteCO2PerYear.ToString("0.##");
                lbl_T_IcTonCO2Yr.Text = calc.TotalWastePlanesCO2PerYear.ToString("0.##");

                pnlMenuDisabled.Visible = false;
                pnlMenuEnabled.Visible = true;

                pnlWaste1Results.Visible = true;
                pnlWasteEntry.Visible = false;
            }
        }
        if (!hasPupilNumbersEntered)
        {
            Response.Redirect(AcmsContext.CurrentPost.Parent.Url);
        }
        base.DataBind();
    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (IsValid)
        {
            ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
            var audits = ClimateChangeChampionsData.CarbonCalculation.GetAllByUserId(context, User.Identity.User.Id);
            var calc = audits.Where(x => x.Audit == AuditNo).SingleOrDefault();

            if (calc == null && audits.Count >= 1)
            {
                calc = new ClimateChangeChampionsData.CarbonCalculation();
                calc.Audit = AuditNo;
                calc.NoPupils = audits.First().NoPupils;
                calc.UserId = User.Identity.User.Id;
                context.CarbonCalculations.InsertOnSubmit(calc);
            }

            if (calc != null)
            {
                calc.LandPaper= float.Parse(txt_L_Paper.Text.Trim());
                calc.LandCard = float.Parse(txt_L_Card.Text.Trim());
                calc.LandFood = float.Parse(txt_L_KFW.Text.Trim());
                calc.LandBottles = float.Parse(txt_L_PBC.Text.Trim());
                calc.LandPackaging = float.Parse(txt_L_PP.Text.Trim());
                calc.LandGlass = float.Parse(txt_L_Glass.Text.Trim());
                calc.LandCans = float.Parse(txt_L_Cans.Text.Trim());
                calc.LandOther = float.Parse(txt_L_Other.Text.Trim());

                calc.RecylePaper = float.Parse(txt_R_Paper.Text.Trim());
                calc.RecyleCard = float.Parse(txt_R_Card.Text.Trim());
                calc.RecyleFood = float.Parse(txt_R_KFW.Text.Trim());
                calc.RecyleBottles = float.Parse(txt_R_PBC.Text.Trim());
                calc.RecylePackaging = float.Parse(txt_R_PP.Text.Trim());
                calc.RecyleGlass = float.Parse(txt_R_Glass.Text.Trim());
                calc.RecyleCans = float.Parse(txt_R_Cans.Text.Trim());
                calc.RecyleOther = float.Parse(txt_R_Other.Text.Trim());
                calc.WasteAuditCompleted = true;
                context.SubmitChanges();
                DataBind();
            }
        }
    }

    protected void btnEditData_Click(object sender, EventArgs e)
    {
        pnlWaste1Results.Visible = false;
        pnlWasteEntry.Visible = true;
    }

    protected override object SaveViewState()
    {
        ViewState["AuditNo"] = AuditNo;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        AuditNo = (int)ViewState["AuditNo"];

    }
}

